import 'package:flutter/material.dart';
import 'package:flutter/services.dart';

class PlatformPage  extends StatelessWidget{
  // flutter注册MethodCallHandler供Native调用方法与android中注册一样。
  Future<dynamic> onMethodCall(MethodCall call) async {
    switch (call.method) {
      case "native-invoke-flutter":
        var argument = call.arguments;
        print("flutter方法被调用==============dyp");
        return "flutter 获得结果返回给native"; // 这里是Flutter返回结果给 native;
      default:
        return "";
    }
  }
  /// 远程调用需要异步操作, 把这个方法生命成 async
  static void toNative() async {
    // 处理异常
    try {
      // channel_name这个时渠道的名称必须与Android端保持一致
      // flag_name与android端的MethodCall.method字符串一一对应；
      print("flutter调用android==============dyp");
      await MethodChannel('channel_name').invokeMethod('flag_name');
    } on PlatformException catch (e) {
    }
  }
  @override
  Widget build(BuildContext context) {
    MethodChannel _channel = new MethodChannel("channel_name");
    _channel.setMethodCallHandler(onMethodCall);
    return Scaffold(
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Text(
              '跨平台调用',
            ),
            ElevatedButton(
              child: Text("normal"),
              onPressed: () {
                toNative();
              },)
          ],
        ),
      ),
    );
  }
}